package cn.com.backstage.controller.base;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_stats;

import cn.com.api.service.ApiT_statsService;

import cn.com.util.*;
import cn.com.util.*;

public class T_statsBaseController {

	private static Logger logger = Logger
			.getLogger(T_statsBaseController.class);

	@Autowired
	private ApiT_statsService apiT_statsService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_stats-list")
	public ModelAndView t_stats_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_stats_list.jsp");

		List<ApiT_stats> t_statss = apiT_statsService.findT_statss();
		mv.addObject("t_statss", t_statss);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_stats-add-pre")
	public ModelAndView t_stats_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_stats_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_stats t_stats = apiT_statsService.findT_statsById(Integer
					.valueOf(id));
			mv.addObject("t_stats", t_stats);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_stats-add")
	public void t_stats_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String stats_id = request.getParameter("stats_id");
		String shop_id = request.getParameter("shop_id");
		String shipper_id = request.getParameter("shipper_id");
		String order_count = request.getParameter("order_count");
		String orders_money = request.getParameter("orders_money");
		String year = request.getParameter("year");
		String month = request.getParameter("month");
		String week = request.getParameter("week");
		String day = request.getParameter("day");
		String order_ontime_count = request.getParameter("order_ontime_count");
		String order_overtime_count = request
				.getParameter("order_overtime_count");
		String order_aheadtime_count = request
				.getParameter("order_aheadtime_count");
		String is_stat = request.getParameter("is_stat");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_stats t_stats = new ApiT_stats();
			/*
			 * t_stats.setStats_id(stats_id); t_stats.setShop_id(shop_id);
			 * t_stats.setShipper_id(shipper_id);
			 * t_stats.setOrder_count(order_count);
			 * t_stats.setOrders_money(orders_money); t_stats.setYear(year);
			 * t_stats.setMonth(month); t_stats.setWeek(week);
			 * t_stats.setDay(day);
			 * t_stats.setOrder_ontime_count(order_ontime_count);
			 * t_stats.setOrder_overtime_count(order_overtime_count);
			 * t_stats.setOrder_aheadtime_count(order_aheadtime_count);
			 * t_stats.setIs_stat(is_stat);
			 */
			apiT_statsService.insertT_stats(t_stats);
		}
		// edit
		else {
			ApiT_stats t_stats = apiT_statsService.findT_statsById(Integer
					.valueOf(id));
			/*
			 * t_stats.setStats_id(stats_id); t_stats.setShop_id(shop_id);
			 * t_stats.setShipper_id(shipper_id);
			 * t_stats.setOrder_count(order_count);
			 * t_stats.setOrders_money(orders_money); t_stats.setYear(year);
			 * t_stats.setMonth(month); t_stats.setWeek(week);
			 * t_stats.setDay(day);
			 * t_stats.setOrder_ontime_count(order_ontime_count);
			 * t_stats.setOrder_overtime_count(order_overtime_count);
			 * t_stats.setOrder_aheadtime_count(order_aheadtime_count);
			 * t_stats.setIs_stat(is_stat);
			 */
			//apiT_statsService.updateT_stats(t_stats);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_stats-delete")
	public void t_stats_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_statsService.deleteT_stats(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
